package com.ugrow.ugrowcommunity;

import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.ugrow.ugrowcommunity.databinding.ActivityMainBinding;
import com.ugrow.ugrowcommunity.vm.forum.ForumFragment;
import com.ugrow.ugrowcommunity.vm.home.HomeFragment;
import com.ugrow.ugrowcommunity.vm.mine.MineFragment;
import com.ugrow.ugrowcommunity.vm.schedule.ScheduleFragment;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class MainActivity extends AppCompatActivity {


    private ActivityMainBinding binding;
    private long lastTime = 0;
    private static final long KEY_DELAY_TIME = 2000;

    private StuVIewPagerAdapter stuViewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initFragment();

    }

    private void initFragment(){
        binding. rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                binding.stuviewpager.setCurrentItem(group.indexOfChild(group.findViewById(checkedId)));
            }
        });
        binding.home.setChecked(true);

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(HomeFragment.newInstance());
        fragments.add(ScheduleFragment.newInstance());
        fragments.add(ForumFragment.newInstance());
        fragments.add(MineFragment.newInstance());

        stuViewPagerAdapter = new StuVIewPagerAdapter(this.getSupportFragmentManager(), fragments);
        binding.stuviewpager.setOffscreenPageLimit(4);
        binding.stuviewpager.setAdapter(stuViewPagerAdapter);
        binding.stuviewpager.setCurrentItem(0);

        binding.stuviewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        binding.home.setChecked(true);
                        break;
                    case 1:
                        binding.schedule.setChecked(true);
                        break;
                    case 2:
                        binding.forum.setChecked(true);
                        break;
                    case 3:
                        binding.mine.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayerStandard.releaseAllVideos();
    }


    @Override
    public void onBackPressed() {
        if (JCVideoPlayerStandard.backPress()) {
            return;
        }
        if (System.currentTimeMillis() - lastTime < KEY_DELAY_TIME) {
            super.onBackPressed();
        } else {
            Toast.makeText(this, "再次点击退出", Toast.LENGTH_SHORT).show();
            lastTime = System.currentTimeMillis();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            JCVideoPlayerStandard.releaseAllVideos();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}